#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;

struct status{
    string str;
    int cost;
    status(string s,int c):str(s),cost(c){}
};

void BFS(string str,int n){
    queue<status> myQueue;
    vector<string> flag;
    myQueue.push(status(str,0));
    while (!myQueue.empty()){
        status current=myQueue.front();
        myQueue.pop();
        if(current.str.find("2012")!=string::npos){
            cout<<current.cost<<endl;
            return;
        }
        for(int i=0;i<n;i++){
            if(i==0){
                status next=current;
                swap(next.str[i],next.str[i+1]);
                next.cost++;
                if(std::count(flag.begin(), flag.end(), next.str)==1){
                    continue;
                }
                myQueue.push(next);
                continue;
            }else if(i==n-1){
                status next=current;
                swap(next.str[i],next.str[i-1]);
                next.cost++;
                if(std::count(flag.begin(), flag.end(), next.str)==1){
                    continue;
                }
                myQueue.push(next);
                continue;
            }
            for(int j=0;j<2;j++){
                status next=current;
                if(j==0){
                    swap(next.str[i],next.str[i+1]);
                }else if(j==1){
                    swap(next.str[i],next.str[i-1]);
                }
                next.cost++;
                if(std::count(flag.begin(), flag.end(), next.str)==1){
                    continue;
                }
                myQueue.push(next);
            }
        }
        flag.push_back(current.str);
    }
}
int main(){
    int n;
    string str;
    while(cin>>n>>str){
//        if(count(str.begin(), str.end(), '2')<=1){
//            cout<<-1<<endl;
//            continue;
//        }else if(str.find('0')==string::npos){
//            cout<<-1<<endl;
//            continue;
//        }else if(str.find('1')==string::npos){
//            cout<<-1<<endl;
//            continue;
//        }
        BFS(str,n);
    }
    return 0;
}